package com.xauto.pioneer.appuser.activity;

import android.content.Intent;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import com.xauto.pioneer.appuser.MainActivity;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.interfaces.CommonCallBack;
import com.xauto.pioneer.appuser.interfaces.ConfigCallBack;
import com.xauto.pioneer.appuser.interfaces.TokenCallBack;
import com.xauto.pioneer.appuser.model.BannerInfo;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.CheckNetWorkUtil;
import com.xauto.pioneer.appuser.util.DataCheckUtil;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.PackageUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.view.PromptDialog;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import cn.jpush.android.api.JPushInterface;

/***
 * 闪屏
 * @author Lemon Thu 9 Feb 2017 15:44
 */
public class LaunchActivity extends BaseActivity {

    @Bind(R.id.tvLaunchVersion) TextView tvLaunchVersion;
    private PromptDialog promptDialog;

    @Override
    protected int getContentView() {
        return R.layout.ac_launch;
    }

    @Override
    protected void initView() {
        tvLaunchVersion.setText("Version " + PackageUtil.getAppVersionName(mContext));

        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout),getString(R.string.dialogpic_notice), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
                startActivity(new Intent(Settings.ACTION_SETTINGS));
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
                finish();
                System.exit(0);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        //有网的情况下获取配置信息保存到本地
        if(checkNetwork()) {
            DataLoadUtil.getConfig(mContext, new ConfigCallBack() {
                @Override
                public void getConfig() {
                    switchPageForLoadBanner();
                }
            });
        }
    }

    /**
     * 跳转到广告业
     */
    private void skipADView(){
        if (!DataCheckUtil.isLogin(mContext)){
            Intent intent = new Intent(mContext, AdvertActivity.class);
            intent.putExtra("banner_info", bannerInfo);
            startActivity(intent);
            finish();
        }else {
            //更新token
            DataLoadUtil.loginWithToken(mContext, new TokenCallBack() {
                @Override
                public void getToken(String token) {
                    //设置推送信息
                    DataLoadUtil.editPushDevice(mContext, JPushInterface.getRegistrationID(mContext), new CommonCallBack() {
                        @Override
                        public void getCallBack() {
                            Intent intent = new Intent(mContext, AdvertActivity.class);
                            intent.putExtra("banner_info", bannerInfo);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
            });
        }
    }
    private void switchPage() {
        if (!DataCheckUtil.isLogin(mContext)){
            skipMainAction();
        }else {
            //更新token
            DataLoadUtil.loginWithToken(mContext, new TokenCallBack() {
                @Override
                public void getToken(String token) {
                    //设置推送信息
                    DataLoadUtil.editPushDevice(mContext, JPushInterface.getRegistrationID(mContext), new CommonCallBack() {
                        @Override
                        public void getCallBack() {
                            skipMainAction();
                        }
                    });
                }
            });
        }
    }



    private Timer mTimer;

    private void switchPageForLoadBanner() {
        if(mTimer == null) {
            mTimer = new Timer();
        }
        loadBanner();
    }

    /*** 加载广告页面，如没有，直接跳过 ****/
    private BannerInfo bannerInfo;

    void loadBanner() {
        new LHttpLib().getIndexBanner(mContext, new BaseLHttpHandler(mContext, false, false) {

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                bannerInfo = new BannerInfo().parse(jsonStatus.data.optJSONObject("banner_info"));
                if(bannerInfo != null && bannerInfo.image != null && !StringUtils.isBlank(bannerInfo.image.source)) {
                    mTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            skipADView();
                        }
                    }, 1000);
                } else {
                    //广告无，直接跳过
                    mTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            switchPage();
                        }
                    }, 1000);
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                //广告无，直接跳过
                mTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        switchPage();
                    }
                }, 1000);
            }
        });
    }

    /**
     * 跳转到主界面
     */
    private void  skipMainAction(){
        startActivity(new Intent(mContext, MainActivity.class));
        finish();
    }
    private boolean checkNetwork() {
        if (!CheckNetWorkUtil.isNetwork(this)) {
            promptDialog.show();
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        if(mTimer!=null){
            mTimer.cancel();
        }
        mTimer = null;
        super.onDestroy();
    }
}
